import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.*;

/**
 * View for sudoku game
 * @author Jiajie Wu
 *
 */
@SuppressWarnings("serial")
public class View extends JFrame implements ChangeListener {
	private Model model;
	
	/**
	 * Construct a frame
	 * @param m Model
	 */
	public View(Model m) {
		model = m;
		int[][] sResult = model.getSudoku();
		
		setLayout(new GridLayout(9, 9));
		
		
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				if (sResult[i][j] == 0) {
					add(new JButton());
				} else {
					String s = Integer.toString(sResult[i][j]);
					add(new JButton(s));
				}
			}
		}
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setVisible(true);
	}
	
	/**
	 * Show the sloved sudoku game
	 */
	@Override
	public void stateChanged(ChangeEvent e) {
		getContentPane().removeAll();
		revalidate();
		setLayout(new GridLayout(9, 9));
		int[][] sResult = model.getSudoku();
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				if (sResult[i][j] == 0) {
					add(new JButton());
				} else {
					String s = Integer.toString(sResult[i][j]);
					add(new JButton(s));
				}
			}
		}
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setVisible(true);
		setSize(400, 400);
		
		repaint();
	}
}
